/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.menu.classDiagram;

import gui.commands.classDiagramCommands.AddNewMethodCommand;
import gui.commands.CommandManager;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author michau
 */
public class AddMethodForm  extends JFrame implements ActionListener {

    private JTextField nameField;
    private JTextField typeField;
    private JTextField argumentsField;
    private JCheckBox isStaticCheckBox;
    private JComboBox visibilityComboBox;
    private JButton    addButton;
    private JButton    cancelButton;

    public AddMethodForm() {
        super("Add new method");

        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout(0,2));

        contentPane.add(new JLabel("Name:"));
        nameField = new JTextField();
        contentPane.add(nameField);
        
        contentPane.add(new JLabel("Type:"));
        typeField = new JTextField();
        contentPane.add(typeField);
        
        contentPane.add(new JLabel("Arguments (separated with ;)"));
        argumentsField = new JTextField();
        contentPane.add(argumentsField);
        
        contentPane.add(new JLabel("Visibility:"));
        visibilityComboBox = new JComboBox();
        visibilityComboBox.addItem("public");
        visibilityComboBox.addItem("private");
        visibilityComboBox.addItem("protected");
        contentPane.add(visibilityComboBox);
        
        contentPane.add(new JLabel("Static?"));
        isStaticCheckBox = new JCheckBox();
        contentPane.add(isStaticCheckBox);

        addButton = new JButton("Add");
        addButton.addActionListener(this);
        contentPane.add(addButton);

        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        contentPane.add(cancelButton);
       
        setVisible(true);
        setSize(getPreferredSize());
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();

        if(button == addButton){
            String name = nameField.getText();

            if(!name.isEmpty())
            {
                String unknown = "unknown";
                String type = typeField.getText();
                boolean isStatic = isStaticCheckBox.getModel().isSelected();
                String visibility = visibilityComboBox.getSelectedItem().toString();
                
                visibility = visibility.equals("public") ? "+" : visibility.equals("private") 
                        ? "-" : visibility.equals("protected") ? "#" : unknown;
                
                String[] arguments;

	            if (argumentsField.getText().contains(";"))
		            arguments = argumentsField.getText().split(";");
	            else {
	                arguments = new String[1];
		            arguments[0] = argumentsField.getText();
	            }

	            CommandManager.getInstance().addAndExecuteClassDiagramCommand(new AddNewMethodCommand(name, unknown,
			            type, isStatic, visibility, arguments));
            }
        }

        setVisible(false);
    }
}
